#!/bin/bash
if [[ $(whoami) != "lfs" ]]; then
	echo "Please log in as user lfs...";
	exit 1;
fi
cd $LFS/sources/pkg || exit 1
source ./utils.sh || exit 2
#######################################
# set package name here
PACKAGE=$(echo $0 | sed -e 's/.*make_\(.*\).sh/\1/');
#######################################
PKG_NAME=$(pkgname basic $PACKAGE); if (($? != 0)); then exit 1; fi;
PKG_VERSION=$(pkgversion basic $PACKAGE); if (($? != 0)); then exit 2; fi;
PKG_ARCHIVE=$(pkgarchive basic $PACKAGE); if (($? != 0)); then exit 3; fi;
echo "make $PACKAGE ($PKG_NAME) with version $PKG_VERSION from archive $PKG_ARCHIVE"
PKG_FOLDER=$(unpack $PKG_ARCHIVE); if (($? != 0)); then exit 4; fi;
PKG_DEST=/usr/pkg/$PKG_NAME/$PKG_VERSION;
cd ../$PKG_FOLDER || exit 5;
#######################################
# prepare package source (patch etc.)
# ...
patch -Np1 -i ../dhcp-$PKG_VERSION-client_script-1.patch

#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package preparing with code $ERR";
    exit 6;
fi
#######################################
# configure and make package
CFLAGS="-D_PATH_DHCLIENT_SCRIPT='\"/sbin/dhclient-script\"'         \
        -D_PATH_DHCPD_CONF='\"/etc/dhcp/dhcpd.conf\"'               \
        -D_PATH_DHCLIENT_CONF='\"/etc/dhcp/dhclient.conf\"'"        \
./configure --prefix=/usr                                           \
            --sysconfdir=/etc/dhcp                                  \
            --localstatedir=/var                                    \
            --with-srv-lease-file=/var/lib/dhcpd/dhcpd.leases       \
            --with-srv6-lease-file=/var/lib/dhcpd/dhcpd6.leases     \
            --with-cli-lease-file=/var/lib/dhclient/dhclient.leases \
            --with-cli6-lease-file=/var/lib/dhclient/dhclient6.leases &&
	make && \
	\
	rm -vRf $PKG_DEST/inst && \
	make DESTDIR=$PKG_DEST/inst -C client install
	

	

#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 6;
fi
#######################################
# make post checks and configuration
# ...
mkdir -vp $PKG_DEST/inst/sbin && \
	mv -v $PKG_DEST/inst/usr/sbin/dhclient $PKG_DEST/inst/sbin && \
	install -v -m755 client/scripts/linux $PKG_DEST/inst/sbin/dhclient-script

#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 7;
fi
echo "# Package information file... generated at make process" > $PKG_DEST/pkg.info && \
echo "# pkg name   source archive    version" >> $PKG_DEST/pkg.info && \
echo "$PKG_NAME  $PKG_ARCHIVE  $PKG_VERSION" >> $PKG_DEST/pkg.info && \
if [ -d $LFS/sources/pkg/basic/$PKG_NAME ]; then cp -aRv $LFS/sources/pkg/basic/$PKG_NAME/* $PKG_DEST; fi && \
cp -avf $LFS/sources/pkg/pkgutils.sh $PKG_DEST/pkgutils.sh && \
tar -czvvf /usr/pkg/${PKG_NAME}_${PKG_VERSION}_pkg.tar.gz -C /usr/pkg/$PKG_NAME $PKG_VERSION && \
rm -Rfv /usr/pkg/$PKG_NAME && \
cd $LFS/sources && \
rm -Rfv $PKG_FOLDER || exit 8;
echo "Build complete basic/$PACKAGE"
echo "Package files are stored to: /usr/pkg/${PKG_NAME}_${PKG_VERSION}_pkg.tar.gz"
echo " --OK--"
